package com.iflytek.springboot_medicine.controller;

import com.iflytek.springboot_medicine.entity.Category;
import com.iflytek.springboot_medicine.entity.Medicine;
import com.iflytek.springboot_medicine.entity.Order;
import com.iflytek.springboot_medicine.entity.User;
import com.iflytek.springboot_medicine.pojo.ResponseMessage;
import com.iflytek.springboot_medicine.service.impl.CategoryServiceImpl;
import com.iflytek.springboot_medicine.service.impl.MedicineServiceImpl;
import com.iflytek.springboot_medicine.service.impl.OrderServiceImpl;
import com.iflytek.springboot_medicine.service.impl.UserServiceImpl;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/web")
@CrossOrigin(origins = "*")
public class WebController {
    private final UserServiceImpl userServiceImpl;
    private final MedicineServiceImpl medicineServiceImpl;
    private final CategoryServiceImpl categoryServiceImpl;
    private final OrderServiceImpl orderServiceImpl;

    public WebController(UserServiceImpl userServiceImpl, MedicineServiceImpl medicineServiceImpl, CategoryServiceImpl categoryServiceImpl, OrderServiceImpl orderServiceImpl) {
        this.userServiceImpl = userServiceImpl;
        this.medicineServiceImpl = medicineServiceImpl;
        this.categoryServiceImpl = categoryServiceImpl;
        this.orderServiceImpl = orderServiceImpl;
    }

    @GetMapping("/getBarChart")
    public ResponseMessage getBarChart() {
        Map<String, Object> map = new HashMap<>();
        List<User> list = userServiceImpl.searchAllUsers();
        Set<String> departmentNameSet = list.stream().map(User::getRole).collect(Collectors.toSet());
        map.put("department", departmentNameSet); //x轴
        List<Long> countList = new ArrayList<>();
        for (String departmentName : departmentNameSet) {
            long count = list.stream().filter(user -> user.getRole().equals(departmentName)).count();
            countList.add(count);
        }
        map.put("count", countList); //y轴
        return ResponseMessage.success(map);
    }

    @GetMapping("/getPieChart")
    public ResponseMessage getPieChart() {
        Map<String, Object> map = new HashMap<>();
        List<Category> allCategory = categoryServiceImpl.findAllCategory();
        Set<String> name = allCategory.stream().map(Category::getName).collect(Collectors.toSet());
        map.put("category", name);
        Set<Integer> collect = allCategory.stream().map(Category::getId).collect(Collectors.toSet());

        List<Medicine> allMedicine = medicineServiceImpl.findAllMedicine();
        List<Long> countList = new ArrayList<>();
        for (Integer medicineId : collect) {
            long count = allMedicine.stream().filter(medicine -> medicine.getCate_id() == medicineId).count();
            countList.add(count);
        }
        map.put("count", countList);
        return ResponseMessage.success(map);
    }

    @GetMapping("/getcategory")
    public ResponseMessage getCategory() {
        Map<String, Object> resultMap = new HashMap<>();
        List<Order> allOrders = orderServiceImpl.findAllOrders();

        // 以名称为key，金额相加后存储到map
        Map<String, Double> nameToTotalPrice = new HashMap<>();
        for (Order order : allOrders) {
            String name = order.getUserName();
            double price = order.getTotalAmount();
            nameToTotalPrice.merge(name, price, Double::sum);
        }

        resultMap.put("nameToTotalPrice", nameToTotalPrice);
        return ResponseMessage.success(resultMap);
    }

    @GetMapping("/orders")
    public List<Order> findAllOrders() {
        return orderServiceImpl.findAllOrders();
    }
}
